﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KYKJ.Monitor.SystemSetting;

namespace KYKJ.Monitor.SystemSetting
{
    public class CameraInfo
    {
        #region 属性

        /// <summary>
        /// ID
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// IP
        /// </summary>
        public string Ip { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 流类型
        /// </summary>
        public int StreamType { get; set; }

        #endregion

        #region 静态方法

        private static string _dbPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sysconfig.db");

        public static event EventHandler CameraManagerInfoChanged;

        /// <summary>
        /// 得到摄像机信息
        /// </summary>
        /// <returns></returns>
        public static CameraInfo GetCameraInfo()
        {
            SQLiteConnection conn = null;
            SQLiteCommand comm = null;
            try
            {
                CameraInfo info = new CameraInfo();
                conn = new SQLiteConnection();
                conn.ConnectionString = "Data Source=" + _dbPath + ";Pooling=true;FailIfMissing=false";
                comm = new SQLiteCommand();
                comm.Connection = conn;
                string strsql = "SELECT * FROM camera LIMIT 0,1";
                IDataReader reader = SQLiteHelper.ExecuteReader(comm, strsql, null);
                if (reader.Read())
                {
                    info.Id = Convert.ToInt32(reader["id"]);
                    info.Ip = reader["IP"].ToString();
                    info.Port = Convert.ToInt32(reader["port"]);
                    info.StreamType = Convert.ToInt32(reader["streamtype"]);
                    info.User = reader["user"].ToString();
                    byte[] outputu = Convert.FromBase64String(info.User);
                    info.User = Encoding.Default.GetString(outputu);
                    info.Password = reader["pwd"].ToString();
                    byte[] outputp = Convert.FromBase64String(info.Password);
                    info.Password = Encoding.Default.GetString(outputp);
                }

                return info;
            }
            catch
            {
                return null;
            }
            finally
            {
                if (comm != null) comm.Dispose();
                if (conn != null) conn.Close();
            }
        }

        /// <summary>
        /// 更新摄像机信息
        /// </summary>
        /// <param name="camera"></param>
        /// <returns></returns>
        public static bool UpdateCameraInfo(CameraInfo camera)
        {
            try
            {
                string connectionString = "Data Source=" + _dbPath + ";Pooling=true;FailIfMissing=false";
                string strsql = "UPDATE camera SET IP=@ip,port=@port,streamtype=@streamtype,user=@user,pwd=@pwd;";
                SQLiteParameter[] parameters = new SQLiteParameter[]
                {
                    new SQLiteParameter("@ip", DbType.String),
                    new SQLiteParameter("@port", DbType.Int32),
                    new SQLiteParameter("@streamtype", DbType.Int32),
                    new SQLiteParameter("@user", DbType.String),
                    new SQLiteParameter("@pwd", DbType.String)
                };
                parameters[0].Value = camera.Ip;
                parameters[1].Value = camera.Port;
                parameters[2].Value = camera.StreamType;
                parameters[3].Value = camera.User;
                parameters[4].Value = camera.Password;

                int re = SQLiteHelper.ExecuteNonQuery(connectionString, strsql, parameters);
                bool result = re > 0;
                if (result)
                {
                    OnCameraManagerInfoChanged();
                }

                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private static void OnCameraManagerInfoChanged()
        {
            CameraManagerInfoChanged?.Invoke(null, EventArgs.Empty);
        }

        #endregion

    }
}
